package javaMSCRM;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import com.microsoft.schemas.crm._2006.query.AllColumns;
import com.microsoft.schemas.crm._2007.webservices.EntityName;
import com.microsoft.schemas.crm._2007.webservices.RetrieveDocument;
import com.microsoft.schemas.crm._2007.webservices.Systemuser;
import com.microsoft.schemas.crm._2007.webservices.RetrieveDocument.Retrieve;
import net.miginfocom.layout.AC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
/**
 * @author Zoran IVANOV
 *
 */
public class User extends Form {
	Systemuser record = null;
	JTextField 	JTFFirstName=new JTextField(""),
				JTFLastName=new JTextField(""),
				JTFEmail=new JTextField(""),
				JTFJobTitle=new JTextField(""),
				JTFBusinessUnit=new JTextField(""),
				JTFDomainName=new JTextField("");
				
	JButton SaveButton,DeleteButton;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param JavaMSCRM
	 */
	public User(JavaMSCRM JavaMSCRM) {
		super(JavaMSCRM);
	}
	
	/**
	 * @param JavaMSCRM
	 * @param Id
	 */
	public User(JavaMSCRM JavaMSCRM, String Id) {
		super(JavaMSCRM, Id);
		try {
	        RetrieveDocument rd = RetrieveDocument.Factory.newInstance();  
	        Retrieve r = Retrieve.Factory.newInstance();	        
	        r.setEntityName(EntityName.SYSTEMUSER.toString());
	        r.setId(Id);
	        r.setColumnSet(AllColumns.Factory.newInstance());	        
	        rd.setRetrieve(r);
	        record = (Systemuser) JavaMSCRM.stub.Retrieve(rd,  JavaMSCRM.catd, null, null).getRetrieveResponse().getRetrieveResult();
		}  
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.toString());
		}		
		setTitle("User: " + record.getFullname());
		JScrollPane FormScrollPane = new JScrollPane(setupFields());
		FormScrollPane.setBorder(BorderFactory. createEmptyBorder(0,0,0,0));
		add(FormScrollPane, BorderLayout.CENTER);
		buttonsBar.setVisible(false);
		pack();
		setVisible(true);
	}
	
	/**
	 * @return JPanel
	 */
	private JPanel setupFields(){
		LC layC = new LC().wrap();
		AC colC = new AC().align("right", 0).fill(1, 3).grow(100, 1, 3).align("right", 2).gap("15", 1);
		AC rowC = new AC().index(6).gap("15!").align("top").grow(100, 0);	
		JPanel form = new JPanel(new MigLayout(layC, colC, rowC));		
		JPanel formContainer = new JPanel(new MigLayout(layC, colC, rowC));		
		formContainer.add(form, BorderLayout.NORTH);			
		form.setBorder(BorderFactory. createEmptyBorder(0,0,0,0));
		//Create TextFields
		
		try{JTFFirstName = new JTextField(record.getFirstname());}catch (Exception e) {}
		try{JTFLastName = new JTextField(record.getLastname());}catch (Exception e) {}
		try{JTFJobTitle = new JTextField(record.getJobtitle());}catch (Exception e) {}
		try{JTFEmail = new JTextField(record.getInternalemailaddress());}catch (Exception e) {}
		try{JTFDomainName = new JTextField(record.getDomainname());}catch (Exception e) {}
		try{JTFBusinessUnit = new JTextField(record.getBusinessunitid().getName());}catch (Exception e) {}
		
		JTFFirstName.setEnabled(false);
		JTFLastName.setEnabled(false);
		JTFJobTitle.setEnabled(false);
		JTFEmail.setEnabled(false);
		JTFDomainName.setEnabled(false);
		JTFBusinessUnit.setEnabled(false);

		addSeparator(form, "General");		
		
		form.add(new JavaMSCRMLabel("First Name",false));		
		form.add(JTFFirstName, "w 200:100:");
		form.add(new JavaMSCRMLabel("Last Name",false));
		form.add(JTFLastName, "w 200:100:");
		form.add(new JavaMSCRMLabel("Domain Logon Name",false));		
		form.add(JTFDomainName, "w 200:100:");
		form.add(new JavaMSCRMLabel("Business Unit",false));		
		form.add(JTFBusinessUnit, "w 200:100:");				
		form.add(new JavaMSCRMLabel("Job Title",false));
		form.add(JTFJobTitle, "w 200:100:");
		form.add(new JavaMSCRMLabel("E-mail",false));
		form.add(JTFEmail, "w 200:100:");
		
		return formContainer;
	}
}
